package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.excelEntity.TranscriptScoreExcel;
import com.xmy.cultivate.entity.views.*;
import com.xmy.cultivate.mapper.*;
import com.xmy.cultivate.respons.ResTranscriptScore;
import com.xmy.cultivate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import static com.xmy.cultivate.util.CommonUtil.validateNumber;

/**
 * <p>
 * 成绩管理 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2023-04-14
 */
@Slf4j
@Service
public class TranscriptServiceImpl extends ServiceImpl<TranscriptMapper, Transcript> implements ITranscriptService {


    @Autowired
    @Lazy
    GradeMapper gradeMapper;

    @Autowired
    @Lazy
    CourseMapper courseMapper;

    @Autowired
    @Lazy
    TranscriptMapper transcriptMapper;

    @Autowired
    TranscriptSetMapper transcriptSetMapper;

    @Autowired
    @Lazy
    IStudentAtSchoolService iStudentAtSchoolService;

    @Autowired
    @Lazy
    IQuarterService iQuarterService;

    @Autowired
    IOrganizationService iOrganizationService;

    @Autowired
    @Lazy
    IStudentAtSchoolScoreDivideService iStudentAtSchoolScoreDivideService;

    @Autowired
    @Lazy
    IStudentClassTypeService iStudentClassTypeService;

    @Autowired
    @Lazy
    ICourseTypeService iCourseTypeService;

    @Autowired
    @Lazy
    IStudentGradeService iStudentGradeService;

    @Autowired
    @Lazy
    IStudentService iStudentService;

    @Autowired
    @Lazy
    ISubjectsService iSubjectsService;

    @Autowired
    @Lazy
    IGradeService iGradeService;

    @Autowired
    @Lazy
    IWeiChartService iWeiChartService;

    @Autowired
    ITranscriptSafetyScoreSetService iTranscriptSafetyScoreSetService;

    @Autowired
    ITranscriptSafetyDiffSetService iTranscriptSafetyDiffSetService;

    @Autowired
    IMqPushService iMqPushService;

    @Autowired
    StudentReadSchoolMapper studentReadSchoolMapper;

    @Override
    public List<StudentTranscript> getEnteringStudent(String[] gradeIdList, Quarter quarter, TranscriptSet transcriptSet) {

        List<StudentTranscript> studentTranscriptList = new ArrayList<>();
        for (String gradeId : gradeIdList) {
            Grade grade = iGradeService.getById(gradeId);
            Course course = courseMapper.selectById(grade.getCourseId());

            QueryWrapper<StudentGrade> studentGradeQueryWrapper = new QueryWrapper<>();
            studentGradeQueryWrapper.eq("year_part", quarter.getYearPart());
            studentGradeQueryWrapper.eq("quarter_num", quarter.getNum());
            studentGradeQueryWrapper.eq("lesson_type", 1);//类型，精品课
            studentGradeQueryWrapper.eq("is_last", 1);
            //studentGradeQueryWrapper.in("year_class_id",1,2,3,4,5,6,7,8,9);
            studentGradeQueryWrapper.eq("grade_id", gradeId);
            studentGradeQueryWrapper.groupBy("student_id");
            List<StudentGrade> studentGradeList = iStudentGradeService.list(studentGradeQueryWrapper);
            List<StudentTranscriptItem> studentTranscriptItemList = new ArrayList<>();
            for (StudentGrade studentGrade : studentGradeList) {
                Student student = iStudentService.getById(studentGrade.getStudentId());
                if (student == null) {
                    continue;
                }
                StudentTranscriptItem studentTranscriptItem = new StudentTranscriptItem();
                studentTranscriptItem.setId(student.getId());
                studentTranscriptItem.setName(student.getName());
                studentTranscriptItem.setHeadImg(student.getHeadImg());
                studentTranscriptItem.setYearClassId(studentGrade.getYearClassId());
                studentTranscriptItem.setGradeSection(CommonUtil.getGradeSectionForYearClassId(studentGrade.getYearClassId()));
                studentTranscriptItem.setSchoolId(studentGrade.getSchoolId());
                List<Subjects> subjectsList = iSubjectsService.getTranscriptSubjects(studentGrade.getYearPart(), studentGrade.getQuarterNum(), studentGrade.getYearClassId(), transcriptSet.getTeacherExamNum());
                List<SubjectsScore> subjectsScoreList = new ArrayList<>();
                for (Subjects subjects : subjectsList) {
//                    if (!subjects.getId().equals(course.getSubjectsId())) {
//                        continue;
//                    }
                    SubjectsScore subjectsScore = new SubjectsScore();
                    subjectsScore.setSubjectsId(subjects.getId());
                    subjectsScore.setSubjectsName(subjects.getName());
                    Transcript transcript = this.getOneForQuarter(quarter.getYearPart(), quarter.getNum(), subjects.getId(), student.getId(), transcriptSet.getTeacherExamNum());
                    if (transcript != null) {
                        subjectsScore.setScore(transcript.getScore());
                        subjectsScore.setReadingSchoolId(transcript.getReadingSchoolId());
                        studentTranscriptItem.setReadingSchoolId(transcript.getReadingSchoolId());
                    }
                    subjectsScoreList.add(subjectsScore);
                }
                studentTranscriptItem.setSubjectsScoreList(subjectsScoreList);
                studentTranscriptItemList.add(studentTranscriptItem);
            }
            StudentTranscript studentTranscript = new StudentTranscript();

            if (grade != null) {
                studentTranscript.setGradeName(grade.getName());
            }


            studentTranscript.setYearPart(quarter.getYearPart());
            studentTranscript.setQuarterNum(quarter.getNum());
            studentTranscript.setExamName(transcriptSet.getTeacherExamName());
            studentTranscript.setStudentTranscriptItemList(studentTranscriptItemList);
            studentTranscriptList.add(studentTranscript);
        }
        return studentTranscriptList;
    }

    public Transcript getOneForQuarter(Integer yearPart, Integer quarterNum, Long subjectsId, Long studentId, Integer examNum) {
        QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();
        transcriptQueryWrapper.eq("year_part", yearPart);
        transcriptQueryWrapper.eq("quarter_num", quarterNum);
        transcriptQueryWrapper.eq("subjects_id", subjectsId);
        transcriptQueryWrapper.eq("student_id", studentId);
        transcriptQueryWrapper.eq("exam_num", examNum);
        transcriptQueryWrapper.last("limit 1");
        Transcript transcript = this.getOne(transcriptQueryWrapper);
        return transcript;
    }


    /**
     * 成绩数据（原本）
     *
     * @param schoolId    校区id
     * @param subjectsId  科目id
     * @param yearPart
     * @param quarterNum
     * @param examNum
     * @param yearClassId
     * @param type
     */
    @Override
    public List<ScoreData> scoreData(String schoolId, List<Long> schoolIdList, String subjectsId, String yearPart, String quarterNum, String examNum, String gradeSection, String yearClassId, String type, String include, String courseTypeId, String dataType) {
        Integer upExamNum = 3;//期末

        /*Integer upYearPart = Integer.parseInt(yearPart);
        Integer upQuarterNum = 0;

        if (quarterNum.equals("2")) {
            upQuarterNum = 4;
            upYearPart = upYearPart - 1;
        } else if (quarterNum.equals("4")) {
            upQuarterNum = 2;
        } else if (quarterNum.equals("1")) {
            upQuarterNum = 4;
            upYearPart = upYearPart - 1;
        } else if (quarterNum.equals("3")) {
            upQuarterNum = 2;
        }*/

        if (StringUtils.isBlank(dataType)) {
            dataType = "1";
        }

        QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();

        //1、个人，2、校区老师，3、校长，4、校区
        if (type.equals("1")) {
            transcriptQueryWrapper.groupBy("teacher_id");
        } else if (type.equals("2")) {
            transcriptQueryWrapper.groupBy("teacher_id", "school_id");
        } else if (type.equals("3")) {
            //transcriptQueryWrapper.groupBy("teacher_id","school_id");
            transcriptQueryWrapper.groupBy("teacher_id");
            transcriptQueryWrapper.inSql("teacher_id", "select admin_id from role_admin where role_id = 1568160887369994242 ");
        } else if (type.equals("4")) {
            transcriptQueryWrapper.groupBy("school_id");
        }

        transcriptQueryWrapper.inSql(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        if (StringUtils.isNotBlank(schoolId)) {
            transcriptQueryWrapper.eq("school_id", schoolId);
        } else {
            transcriptQueryWrapper.in("school_id", schoolIdList);
        }

        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeSection), "grade_section", gradeSection);
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
        if (StringUtils.isNotBlank(gradeSection) && gradeSection.equals("3")) {
            transcriptQueryWrapper.in("year_class_id", 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);//只统计3到8年级的学生
        } else {
            transcriptQueryWrapper.in("year_class_id", 3, 4, 5, 6, 7, 8, 9);//只统计3到8年级的学生,10,11,12
        }

        Quarter quarter = iQuarterService.getQuarterForYearAndNum(Integer.parseInt(yearPart), Integer.parseInt(quarterNum));
        TranscriptSafetyScoreSet tsSet11 = new TranscriptSafetyScoreSet();//英语小学
        TranscriptSafetyScoreSet tsSet12 = new TranscriptSafetyScoreSet();//英语初中
        TranscriptSafetyScoreSet tsSet21 = new TranscriptSafetyScoreSet();//数学小学
        TranscriptSafetyScoreSet tsSet22 = new TranscriptSafetyScoreSet();//数学初中
        TranscriptSafetyScoreSet tsSet32 = new TranscriptSafetyScoreSet();//物理初中

        QueryWrapper<TranscriptSafetyScoreSet> transcriptSafetyScoreSetQueryWrapper = new QueryWrapper<>();
        if (dataType.equals("1")) {
            transcriptSafetyScoreSetQueryWrapper.le("quarter_id_start", quarter.getId());
            transcriptSafetyScoreSetQueryWrapper.ge("quarter_id_end", quarter.getId());
        } else if (dataType.equals("4")) {// 成绩数据4
            transcriptSafetyScoreSetQueryWrapper.le("quarter_id_start", 16);
            transcriptSafetyScoreSetQueryWrapper.ge("quarter_id_end", 16);
        }
        List<TranscriptSafetyScoreSet> transcriptSafetyScoreSetList = iTranscriptSafetyScoreSetService.list(transcriptSafetyScoreSetQueryWrapper);
        for (TranscriptSafetyScoreSet transcriptSafetyScoreSet : transcriptSafetyScoreSetList) {
            if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet11 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet12 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet21 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet22 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(3L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet32 = transcriptSafetyScoreSet;
            }
        }

        String examNumParam = examNum;//查询安全值设置使用
        if (!examNum.equals("1") && !examNum.equals("3")) {
            examNumParam = "3";//因为只配置了期中与期末的，因此其它的就设置为使用期末的
        }

        QueryWrapper<TranscriptSafetyDiffSet> safetyDiffSetQueryWrapper = new QueryWrapper<>();
        if (dataType.equals("1")) {
            safetyDiffSetQueryWrapper.le("quarter_id_start", quarter.getId());
            safetyDiffSetQueryWrapper.ge("quarter_id_end", quarter.getId());
        } else if (dataType.equals("4")) {// 成绩数据4
            safetyDiffSetQueryWrapper.le("quarter_id_start", 16);
            safetyDiffSetQueryWrapper.ge("quarter_id_end", 16);
        }
        safetyDiffSetQueryWrapper.eq("exam_num", examNumParam);
        safetyDiffSetQueryWrapper.apply("FIND_IN_SET(" + quarterNum + ",quarter_num_str)");

        List<TranscriptSafetyDiffSet> transcriptSafetyDiffSetList = iTranscriptSafetyDiffSetService.list(safetyDiffSetQueryWrapper);
        for (TranscriptSafetyDiffSet tsSet : transcriptSafetyDiffSetList) {
            Quarter upQuarter = iQuarterService.getUpQuarter(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), tsSet.getContrastDiffNum());
            tsSet.setUpYearPart(upQuarter.getYearPart());
            tsSet.setUpQuarterNum(upQuarter.getNum());
            tsSet.setExamNum(Integer.parseInt(examNum));
            tsSet.setUpExamNum(tsSet.getContrastExamNum());
        }
        List<ScoreData> scoreDataList = new ArrayList<>();
        if (transcriptSafetyDiffSetList.size() == 0) {
            return scoreDataList;
        }

        scoreDataList = transcriptMapper.scoreData(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), Integer.parseInt(examNum), Integer.valueOf(include), courseTypeId, transcriptQueryWrapper, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, transcriptSafetyDiffSetList, dataType, yearClassId);
        for (ScoreData scoreData : scoreDataList) {
            if (scoreData.getStudentAllCount() > 0) {
                scoreData.setNotCounted(scoreData.getStudentAllCount() - scoreData.getStuRealityCount());
                if (scoreData.getStuRealityCount() - scoreData.getNotCalcCount() > 0) {
                    scoreData.setNoPassRate(new BigDecimal(scoreData.getNoPassCount()).divide(new BigDecimal(scoreData.getStuRealityCount() - scoreData.getNotCalcCount()), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));//不及格率
                    scoreData.setPassRate(new BigDecimal(scoreData.getPassCount() - scoreData.getNotCalcPassCount()).divide(new BigDecimal(scoreData.getStuRealityCount() - scoreData.getNotCalcCount()), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));//优秀率
                    scoreData.setCarryRate(new BigDecimal(scoreData.getCarryCount()).divide(new BigDecimal(scoreData.getStuRealityCount() - scoreData.getNotCalcCount()), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));//提分率
                    //scoreData.setRetrogressCount(scoreData.getStuRealityCount() - scoreData.getCarryCount() - scoreData.getPassCount());
                    scoreData.setRetrogressRate(new BigDecimal(scoreData.getRetrogressCount()).divide(new BigDecimal(scoreData.getStuRealityCount() - scoreData.getNotCalcCount()), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));// 退步率
                } else {
                    scoreData.setNoPassRate(new BigDecimal(0));
                    scoreData.setPassRate(new BigDecimal(0));
                    scoreData.setCarryRate(new BigDecimal(0));
                    scoreData.setRetrogressRate(new BigDecimal(0));
                    scoreData.setRetrogressCount(0);
                }
                scoreData.setSecureRate(scoreData.getCarryRate().add(scoreData.getPassRate()));//安全值
            } else {
                scoreData.setNoPassRate(new BigDecimal(0));
                scoreData.setPassRate(new BigDecimal(0));
                scoreData.setCarryRate(new BigDecimal(0));
                scoreData.setSecureRate(new BigDecimal(0));
                scoreData.setRetrogressRate(new BigDecimal(0));
                scoreData.setRetrogressCount(0);
            }
        }

        return scoreDataList;
    }


    @Override
    public List<ScoreDataExcel> scoreDataExcel(String schoolId, List<Long> schoolIdList, String subjectsId, String yearPart, String quarterNum, String examNum, String gradeSection, String yearClassId, String type, String include, String courseTypeId, String dataType) {
        Integer upExamNum = 3;//期末

        /*Integer upYearPart = Integer.parseInt(yearPart);
        Integer upQuarterNum = 0;

        if (quarterNum.equals("2")) {
            upQuarterNum = 4;
            upYearPart = upYearPart - 1;
        } else if (quarterNum.equals("4")) {
            upQuarterNum = 2;
        } else if (quarterNum.equals("1")) {
            upQuarterNum = 4;
            upYearPart = upYearPart - 1;
        } else if (quarterNum.equals("3")) {
            upQuarterNum = 2;
        }*/

        if (StringUtils.isBlank(dataType)) {
            dataType = "1";
        }

        QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();

        //1、个人，2、校区老师，3、校长，4、校区
        if (type.equals("1")) {
            transcriptQueryWrapper.groupBy("teacher_id");
        } else if (type.equals("2")) {
            transcriptQueryWrapper.groupBy("teacher_id", "school_id");
        } else if (type.equals("3")) {
            transcriptQueryWrapper.groupBy("teacher_id");
            transcriptQueryWrapper.inSql("teacher_id", "select admin_id from role_admin where role_id = 1568160887369994242 ");
        } else if (type.equals("4")) {
            transcriptQueryWrapper.groupBy("school_id");
        }

        transcriptQueryWrapper.inSql(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        if (StringUtils.isNotBlank(schoolId)) {
            transcriptQueryWrapper.eq("school_id", schoolId);
        } else {
            transcriptQueryWrapper.in("school_id", schoolIdList);
        }

        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeSection), "grade_section", gradeSection);
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
        if (gradeSection.equals("3")) {
            transcriptQueryWrapper.in("year_class_id", 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);//只统计3到8年级的学生
        } else {
            transcriptQueryWrapper.in("year_class_id", 3, 4, 5, 6, 7, 8, 9);//只统计3到8年级的学生,10,11,12
        }

        Quarter quarter = iQuarterService.getQuarterForYearAndNum(Integer.parseInt(yearPart), Integer.parseInt(quarterNum));
        TranscriptSafetyScoreSet tsSet11 = new TranscriptSafetyScoreSet();//英语小学
        TranscriptSafetyScoreSet tsSet12 = new TranscriptSafetyScoreSet();//英语初中
        TranscriptSafetyScoreSet tsSet21 = new TranscriptSafetyScoreSet();//数学小学
        TranscriptSafetyScoreSet tsSet22 = new TranscriptSafetyScoreSet();//数学初中
        TranscriptSafetyScoreSet tsSet32 = new TranscriptSafetyScoreSet();//物理初中

        QueryWrapper<TranscriptSafetyScoreSet> transcriptSafetyScoreSetQueryWrapper = new QueryWrapper<>();
        if (dataType.equals("1")) {
            transcriptSafetyScoreSetQueryWrapper.le("quarter_id_start", quarter.getId());
            transcriptSafetyScoreSetQueryWrapper.ge("quarter_id_end", quarter.getId());
        } else if (dataType.equals("4")) {// 成绩数据4
            transcriptSafetyScoreSetQueryWrapper.le("quarter_id_start", 16);
            transcriptSafetyScoreSetQueryWrapper.ge("quarter_id_end", 16);
        }
        List<TranscriptSafetyScoreSet> transcriptSafetyScoreSetList = iTranscriptSafetyScoreSetService.list(transcriptSafetyScoreSetQueryWrapper);
        for (TranscriptSafetyScoreSet transcriptSafetyScoreSet : transcriptSafetyScoreSetList) {
            if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet11 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet12 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet21 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet22 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(3L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet32 = transcriptSafetyScoreSet;
            }
        }

        String examNumParam = examNum;//查询安全值设置使用
        if (!examNum.equals("1") && !examNum.equals("3")) {
            examNumParam = "3";//因为只配置了期中与期末的，因此其它的就设置为使用期末的
        }

        QueryWrapper<TranscriptSafetyDiffSet> safetyDiffSetQueryWrapper = new QueryWrapper<>();
        if (dataType.equals("1")) {
            safetyDiffSetQueryWrapper.le("quarter_id_start", quarter.getId());
            safetyDiffSetQueryWrapper.ge("quarter_id_end", quarter.getId());
        } else if (dataType.equals("4")) {// 成绩数据4
            safetyDiffSetQueryWrapper.le("quarter_id_start", 16);
            safetyDiffSetQueryWrapper.ge("quarter_id_end", 16);
        }
        safetyDiffSetQueryWrapper.eq("exam_num", examNumParam);
        safetyDiffSetQueryWrapper.apply("FIND_IN_SET(" + quarterNum + ",quarter_num_str)");

        List<TranscriptSafetyDiffSet> transcriptSafetyDiffSetList = iTranscriptSafetyDiffSetService.list(safetyDiffSetQueryWrapper);
        for (TranscriptSafetyDiffSet tsSet : transcriptSafetyDiffSetList) {
            Quarter upQuarter = iQuarterService.getUpQuarter(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), tsSet.getContrastDiffNum());
            tsSet.setUpYearPart(upQuarter.getYearPart());
            tsSet.setUpQuarterNum(upQuarter.getNum());
            tsSet.setExamNum(Integer.parseInt(examNum));
            tsSet.setUpExamNum(tsSet.getContrastExamNum());
        }
        List<ScoreDataExcel> scoreDataList = new ArrayList<>();
        if (transcriptSafetyDiffSetList.size() == 0) {
            return scoreDataList;
        }

        scoreDataList = transcriptMapper.scoreDataExcel(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), Integer.parseInt(examNum), Integer.valueOf(include), courseTypeId, transcriptQueryWrapper, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, transcriptSafetyDiffSetList, yearClassId);

        for (ScoreDataExcel scoreData : scoreDataList) {
            if (scoreData.getStudentAllCount() > 0) {
                scoreData.setNotCounted(scoreData.getStudentAllCount() - scoreData.getStuRealityCount());
                if (scoreData.getStuRealityCount() - scoreData.getNotCalcCount() > 0) {
                    scoreData.setNoPassRate(new BigDecimal(scoreData.getNoPassCount()).divide(new BigDecimal(scoreData.getStuRealityCount() - scoreData.getNotCalcCount()), 4, BigDecimal.ROUND_HALF_UP));//不及格率
                    scoreData.setPassRate(new BigDecimal(scoreData.getPassCount() - scoreData.getNotCalcPassCount()).divide(new BigDecimal(scoreData.getStuRealityCount() - scoreData.getNotCalcCount()), 4, BigDecimal.ROUND_HALF_UP));//优秀率
                    scoreData.setCarryRate(new BigDecimal(scoreData.getCarryCount()).divide(new BigDecimal(scoreData.getStuRealityCount() - scoreData.getNotCalcCount()), 4, BigDecimal.ROUND_HALF_UP));//优秀率
                    scoreData.setRetrogressRate(new BigDecimal(scoreData.getRetrogressCount()).divide(new BigDecimal(scoreData.getStuRealityCount() - scoreData.getNotCalcCount()), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));// 退步率
                } else {
                    scoreData.setNoPassRate(new BigDecimal(0));
                    scoreData.setPassRate(new BigDecimal(0));
                    scoreData.setCarryRate(new BigDecimal(0));
                    scoreData.setRetrogressRate(new BigDecimal(0));
                    scoreData.setRetrogressCount(0);
                }
                scoreData.setSecureRate(scoreData.getCarryRate().add(scoreData.getPassRate()));//安全值
            } else {
                scoreData.setNoPassRate(new BigDecimal(0));
                scoreData.setPassRate(new BigDecimal(0));
                scoreData.setCarryRate(new BigDecimal(0));
                scoreData.setSecureRate(new BigDecimal(0));
                scoreData.setRetrogressRate(new BigDecimal(0));
                scoreData.setRetrogressCount(0);
            }
        }

        return scoreDataList;
    }


    public IPage<ScoreDataDetail> scoreDataDetail(IPage<?> page, String schoolId, String subjectsId, String yearPart, String quarterNum, String examNum, String gradeSection, String yearClassId, String type, String teacherId, String gradeId, String isStatistics, String include, String studentName, String courseTypeId, String dataType) {
        Integer upExamNum = 3;//期末

        /*Integer upYearPart = Integer.parseInt(yearPart);
        Integer upQuarterNum = 0;
        if (quarterNum.equals("2")) {
            upQuarterNum = 4;
            upYearPart = upYearPart - 1;
        } else if (quarterNum.equals("4")) {
            upQuarterNum = 2;
        } else if (quarterNum.equals("1")) {
            upQuarterNum = 4;
            upYearPart = upYearPart - 1;
        } else if (quarterNum.equals("3")) {
            upQuarterNum = 2;
        }*/

        if (StringUtils.isBlank(dataType)) {
            dataType = "1";
        }

        QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();

        //1、个人，2、校区老师，3、校长，4、校区
        if (type.equals("1")) {
            //transcriptQueryWrapper.groupBy("teacher_id");
            transcriptQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);
        } else if (type.equals("2")) {
            transcriptQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);
            //transcriptQueryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
            //transcriptQueryWrapper.groupBy("teacher_id","school_id");
        } else if (type.equals("3")) {
            //transcriptQueryWrapper.groupBy("teacher_id","school_id");
            transcriptQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);
            //transcriptQueryWrapper.inSql("teacher_id","select admin_id from role_admin where role_id = 1568160887369994242 ");
        } else if (type.equals("4")) {
            //transcriptQueryWrapper.groupBy("school_id");
            //transcriptQueryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        }
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeSection), "grade_section", gradeSection);
        transcriptQueryWrapper.inSql(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        //transcriptQueryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
        //transcriptQueryWrapper.notIn("grade_section",0,3);
        if (gradeSection.equals("3")) {
            transcriptQueryWrapper.in("year_class_id", 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);//只统计3到8年级的学生
        } else {
            transcriptQueryWrapper.in("year_class_id", 3, 4, 5, 6, 7, 8, 9);//只统计3到8年级的学生,10,11,12
        }


        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeId), "grade_id", gradeId);
        if (StringUtils.isNotBlank(isStatistics)) {
            if (isStatistics.equals("1")) {
                transcriptQueryWrapper.and(i -> i.eq("is_statistics", 1).eq("is_statistics_b", 1).or(j -> j.in("year_class_id", 3).eq("quarter_num", 4).eq("is_statistics", 1)).or(j -> j.eq("year_class_id", 8).eq("quarter_num", 4).eq("subjects_id", 3).eq("is_statistics", 1)));
            } else {
                if (quarterNum.equals("4")) {
                    transcriptQueryWrapper.and(i -> i.eq("is_statistics", 0).or(j -> j.ne("year_class_id", 3).eq("quarter_num", 4).eq("is_statistics_b", 0)).or(j -> j.ne("year_class_id", 8).eq("subjects_id", 3).eq("quarter_num", 4).eq("is_statistics_b", 0)));
                } else {
                    transcriptQueryWrapper.and(i -> i.eq("is_statistics", 0).or(j -> j.eq("is_statistics_b", 0)));
                }//transcriptQueryWrapper.and(i -> i.eq("is_statistics", 0).or(j -> j.ne("year_class_id", 8).eq("subjects_id",3).eq("quarter_num", 4).eq("is_statistics_b", 0)));
            }
        } else {
            //transcriptQueryWrapper.eq("is_statistics",1);//默认为统计
            //transcriptQueryWrapper.eq("is_statistics_b",1);//默认为统计
            //transcriptQueryWrapper.and(i->i.eq("is_statistics",1).eq("is_statistics_b",1).or(j->j.eq("year_class_id",3).eq("quarter_num",4).eq("is_statistics",1)));
        }
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeId), "grade_id", gradeId);

        String examNumParam = examNum;//查询安全值设置使用
        if (!examNum.equals("1") && !examNum.equals("3")) {
            examNumParam = "3";//因为只配置了期中与期末的，因此其它的就设置为使用期末的
        }
        Quarter quarter = iQuarterService.getQuarterForYearAndNum(Integer.parseInt(yearPart), Integer.parseInt(quarterNum));

        QueryWrapper<TranscriptSafetyDiffSet> safetyDiffSetQueryWrapper = new QueryWrapper<>();
        if (dataType.equals("1")) {
            safetyDiffSetQueryWrapper.le("quarter_id_start", quarter.getId());
            safetyDiffSetQueryWrapper.ge("quarter_id_end", quarter.getId());
        } else if (dataType.equals("4")) {// 成绩数据4
            safetyDiffSetQueryWrapper.le("quarter_id_start", 16);
            safetyDiffSetQueryWrapper.ge("quarter_id_end", 16);
        }
        safetyDiffSetQueryWrapper.eq("exam_num", examNumParam);
        safetyDiffSetQueryWrapper.apply("FIND_IN_SET(" + quarterNum + ",quarter_num_str)");

        List<TranscriptSafetyDiffSet> transcriptSafetyDiffSetList = iTranscriptSafetyDiffSetService.list(safetyDiffSetQueryWrapper);
        for (TranscriptSafetyDiffSet tsSet : transcriptSafetyDiffSetList) {
            Quarter upQuarter = iQuarterService.getUpQuarter(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), tsSet.getContrastDiffNum());
            tsSet.setUpYearPart(upQuarter.getYearPart());
            tsSet.setUpQuarterNum(upQuarter.getNum());
            tsSet.setExamNum(Integer.parseInt(examNum));
            tsSet.setUpExamNum(tsSet.getContrastExamNum());
        }

        IPage<ScoreDataDetail> iPage = transcriptMapper.scoreDataDetail(page, Integer.parseInt(yearPart), Integer.parseInt(quarterNum), Integer.parseInt(examNum), Integer.valueOf(include), studentName, courseTypeId, transcriptQueryWrapper, transcriptSafetyDiffSetList, dataType);

        return iPage;
    }

    public IPage<ScoreDataDetail> otherScoreDataDetail(IPage<?> page, String schoolId, String subjectsId, String yearPart, String quarterNum, String examNum, String gradeSection, String yearClassId, String type, String teacherId, String gradeId, String scoreDetailType, String status, String include, String studentName, String courseTypeId, String dataType, String carryStatus) {
        if (StringUtils.isBlank(dataType)) {
            dataType = "1";
        }

        QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeId), "grade_id", gradeId);

        //1、个人，2、校区老师，3、校长，4、校区
        if (type.equals("1")) {
            //transcriptQueryWrapper.groupBy("teacher_id");
            transcriptQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);
        } else if (type.equals("2")) {
            transcriptQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);
            //transcriptQueryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
            //transcriptQueryWrapper.groupBy("teacher_id","school_id");
        } else if (type.equals("3")) {
            //transcriptQueryWrapper.groupBy("teacher_id","school_id");
            transcriptQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);
            //transcriptQueryWrapper.inSql("teacher_id","select admin_id from role_admin where role_id = 1568160887369994242 ");
        } else if (type.equals("4")) {
            //transcriptQueryWrapper.groupBy("school_id");
            //transcriptQueryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        }
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeSection), "grade_section", gradeSection);
        transcriptQueryWrapper.inSql(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        //transcriptQueryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
        //transcriptQueryWrapper.notIn("grade_section",0,3);
        if (gradeSection.equals("3")) {
            transcriptQueryWrapper.in("year_class_id", 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);//只统计3到8年级的学生
        } else {
            transcriptQueryWrapper.in("year_class_id", 3, 4, 5, 6, 7, 8, 9);//只统计3到8年级的学生,10,11,12
        }

        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeId), "grade_id", gradeId);
        //已统计的
        transcriptQueryWrapper.and(i -> i.eq("is_statistics", 1).eq("is_statistics_b", 1).or(j -> j.in("year_class_id", 3).eq("quarter_num", 4).eq("is_statistics", 1)).or(j -> j.in("year_class_id", 8).eq("subjects_id", 3).eq("quarter_num", 4).eq("is_statistics", 1)));
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(gradeId), "grade_id", gradeId);

        Quarter quarter = iQuarterService.getQuarterForYearAndNum(Integer.parseInt(yearPart), Integer.parseInt(quarterNum));
        TranscriptSafetyScoreSet tsSet11 = new TranscriptSafetyScoreSet();//英语小学
        TranscriptSafetyScoreSet tsSet12 = new TranscriptSafetyScoreSet();//英语初中
        TranscriptSafetyScoreSet tsSet21 = new TranscriptSafetyScoreSet();//数学小学
        TranscriptSafetyScoreSet tsSet22 = new TranscriptSafetyScoreSet();//数学初中
        TranscriptSafetyScoreSet tsSet32 = new TranscriptSafetyScoreSet();//物理初中

        QueryWrapper<TranscriptSafetyScoreSet> transcriptSafetyScoreSetQueryWrapper = new QueryWrapper<>();
        if (dataType.equals("1")) {
            transcriptSafetyScoreSetQueryWrapper.le("quarter_id_start", quarter.getId());
            transcriptSafetyScoreSetQueryWrapper.ge("quarter_id_end", quarter.getId());
        } else if (dataType.equals("4")) {// 成绩数据4
            transcriptSafetyScoreSetQueryWrapper.le("quarter_id_start", 16);
            transcriptSafetyScoreSetQueryWrapper.ge("quarter_id_end", 16);
        }
        List<TranscriptSafetyScoreSet> transcriptSafetyScoreSetList = iTranscriptSafetyScoreSetService.list(transcriptSafetyScoreSetQueryWrapper);
        for (TranscriptSafetyScoreSet transcriptSafetyScoreSet : transcriptSafetyScoreSetList) {
            if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet11 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet12 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet21 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet22 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(3L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet32 = transcriptSafetyScoreSet;
            }
        }


        String examNumParam = examNum;//查询安全值设置使用
        if (!examNum.equals("1") && !examNum.equals("3")) {
            examNumParam = "3";//因为只配置了期中与期末的，因此其它的就设置为使用期末的
        }

        QueryWrapper<TranscriptSafetyDiffSet> safetyDiffSetQueryWrapper = new QueryWrapper<>();
        if (dataType.equals("1")) {
            safetyDiffSetQueryWrapper.le("quarter_id_start", quarter.getId());
            safetyDiffSetQueryWrapper.ge("quarter_id_end", quarter.getId());
        } else if (dataType.equals("4")) {// 成绩数据4
            safetyDiffSetQueryWrapper.le("quarter_id_start", 16);
            safetyDiffSetQueryWrapper.ge("quarter_id_end", 16);
        }
        safetyDiffSetQueryWrapper.eq("exam_num", examNumParam);
        safetyDiffSetQueryWrapper.apply("FIND_IN_SET(" + quarterNum + ",quarter_num_str)");

        List<TranscriptSafetyDiffSet> transcriptSafetyDiffSetList = iTranscriptSafetyDiffSetService.list(safetyDiffSetQueryWrapper);
        for (TranscriptSafetyDiffSet tsSet : transcriptSafetyDiffSetList) {
            Quarter upQuarter = iQuarterService.getUpQuarter(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), tsSet.getContrastDiffNum());
            tsSet.setUpYearPart(upQuarter.getYearPart());
            tsSet.setUpQuarterNum(upQuarter.getNum());
            tsSet.setExamNum(Integer.parseInt(examNum));
            tsSet.setUpExamNum(tsSet.getContrastExamNum());
        }

        IPage<ScoreDataDetail> iPage = new Page<>();
        if (transcriptSafetyDiffSetList.size() == 0) {
            return iPage;
        }

        //transcriptQueryWrapper.and(i->i.eq("is_statistics",1).eq("is_statistics_b",1).or(j->j.eq("year_class_id",3).eq("quarter_num",4).eq("is_statistics",1)));

        //1:不及格人数详情，2：优秀人数详情，3提分人数详情
        if (scoreDetailType.equals("1")) {
            //小学：＜60分，初中：＜72分
            if (StringUtils.isBlank(status)) {
                //transcriptQueryWrapper.and(i->i.eq("grade_section",1).lt("score_end",60).or(j->j.eq("grade_section",2).lt("score_end",72)));
                transcriptQueryWrapper.eq(StringUtils.isNotBlank(status), "pass_count", status);
            } else {
                transcriptQueryWrapper.eq(StringUtils.isNotBlank(status), "pass_count", status);
            }
            iPage = transcriptMapper.passsSoreDataDetail(page, Integer.parseInt(yearPart), Integer.parseInt(quarterNum), Integer.parseInt(examNum), Integer.valueOf(include), studentName, courseTypeId, transcriptQueryWrapper, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, transcriptSafetyDiffSetList, dataType);
        } else if (scoreDetailType.equals("2")) {
            //小学：≥90分，初中：≥96分
            if (StringUtils.isBlank(status)) {
                //transcriptQueryWrapper.and(i->i.eq("grade_section",1).ge("score_end",90).or(j->j.eq("grade_section",2).ge("score_end",96)));
                transcriptQueryWrapper.eq(StringUtils.isNotBlank(status), "pass_count", status);
            } else {
                transcriptQueryWrapper.eq(StringUtils.isNotBlank(status), "pass_count", status);
            }

            if (StringUtils.isNotBlank(carryStatus) && carryStatus.equals("0")) { // carryStatus 1为优秀提分 0为优秀退步
                transcriptQueryWrapper.lt("diff_score", 0);
                tsSet32.setPassScore(tsSet32.getRetrogressScore());
            } else {
                //提分
                transcriptQueryWrapper.eq(StringUtils.isNotBlank(carryStatus), "diff_pass_count", carryStatus);
            }

            iPage = transcriptMapper.excellentSoreDataDetail(page, Integer.parseInt(yearPart), Integer.parseInt(quarterNum), Integer.parseInt(examNum), Integer.valueOf(include), studentName, courseTypeId, transcriptQueryWrapper, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, transcriptSafetyDiffSetList, dataType);
        } else if (scoreDetailType.equals("3")) {
            //小学：低于90分，但有提分的学生，初中：低于96分，但有提分的学生
            //退步
            Integer detailType = 1;//提分，2退步
            if (StringUtils.isNotBlank(status) && status.equals("0")) { // status 1为提分 0为退步
                //transcriptQueryWrapper.and(i->i.eq("grade_section",1).lt("score_end",90).or(j->j.eq("grade_section",2).lt("score_end",96)));
                //transcriptQueryWrapper.eq("is_statistics",1);
                //transcriptQueryWrapper.eq("is_statistics_b",1);
                detailType = 2;
                transcriptQueryWrapper.lt("diff_score", 0);
                tsSet32.setPassScore(tsSet32.getRetrogressScore());
            } else {
                //提分
                transcriptQueryWrapper.eq(StringUtils.isNotBlank(status), "pass_count", status);
            }

            iPage = transcriptMapper.carrySoreDataDetail(page, Integer.parseInt(yearPart), Integer.parseInt(quarterNum), Integer.parseInt(examNum), Integer.valueOf(include), studentName, courseTypeId, transcriptQueryWrapper, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32, transcriptSafetyDiffSetList, dataType);
        }
        return iPage;
    }


    public String verifyScore(Student student) {
        Quarter quarter = iQuarterService.getNowQuarter();

        //获取上一个学期
        Quarter lastQuarter = iQuarterService.getLastQuarter(quarter.getYearPart(), quarter.getNum());
        if (lastQuarter.getType().equals(2)) {
            //如果上一学期为假期，再获取上一学期
            lastQuarter = iQuarterService.getLastQuarter(lastQuarter.getYearPart(), lastQuarter.getNum());
        }


        StringBuilder stringBuilder = new StringBuilder();
        Long readingSchoolId = student.getReadingSchoolId();
        Integer yearClassId = student.getYearClassId();

        for (ResTranscriptScore transcript : student.getTranscriptList()) {
            //为字母
            if (StringUtils.isNotBlank(transcript.getScore()) && !validateNumber(transcript.getScore())) {
                StudentAtSchool studentAtSchool = iStudentAtSchoolService.getById(readingSchoolId);
                if (studentAtSchool == null) {
                    stringBuilder.append(student.getResidueCourseCount()).append("学校不存在").append(",");
                } else {
                    Integer gradeSection = CommonUtil.getGradeSectionForYearClassId(yearClassId);
                    QueryWrapper<StudentAtSchoolScoreDivide> studentAtSchoolScoreDivideQueryWrapper = new QueryWrapper<>();
                    studentAtSchoolScoreDivideQueryWrapper.eq("student_at_school_id", readingSchoolId);
                    studentAtSchoolScoreDivideQueryWrapper.eq("grade_section", gradeSection);
                    studentAtSchoolScoreDivideQueryWrapper.eq("score", transcript.getScore().trim());
                    studentAtSchoolScoreDivideQueryWrapper.last("limit 1");
                    StudentAtSchoolScoreDivide studentAtSchoolScoreDivide = iStudentAtSchoolScoreDivideService.getOne(studentAtSchoolScoreDivideQueryWrapper);
                    if (studentAtSchoolScoreDivide == null) {
                        stringBuilder.append(student.getName()).append(transcript.getScore()).append(",系统不存在学校：").append(studentAtSchool.getName()).append("分段").append(",");
                    }
                }
            }
        }

        if (stringBuilder.length() > 0) {
            return stringBuilder.toString();
        } else {
            return "true";
        }
    }

    public boolean addTrascript(Student student) {

        Quarter quarter = iQuarterService.getNowQuarter();

        //获取上一个学期
        Quarter lastQuarter = iQuarterService.getLastQuarter(quarter.getYearPart(), quarter.getNum());
        if (lastQuarter.getType().equals(2)) {
            //如果上一学期为假期，再获取上一学期
            lastQuarter = iQuarterService.getLastQuarter(lastQuarter.getYearPart(), lastQuarter.getNum());
        }

        Long readingSchoolId = student.getReadingSchoolId();
        Integer yearClassId = student.getYearClassId();
        Integer transcriptYearClassId = yearClassId;//填写成绩的年级

        if (lastQuarter.getNum().equals(2)) {//如果上个学期为春季班，年级减1
            transcriptYearClassId = yearClassId - 1;//上个学期年级
        }


        //秋度班初二，物理按数学分数算
        /*String scoreTemp=null;
        Integer scoreTypeTemp=null;
        for (ResTranscriptScore transcript:student.getTranscriptList()){
            if (transcript.getSubjectsId().equals(2L)){
                scoreTemp = transcript.getScore();
                scoreTypeTemp = transcript.getScoreType();
            }
        }

        if (quarter.equals(4) && yearClassId.equals(11)){
            if (scoreTemp!=null&& scoreTypeTemp!=null){
                for (ResTranscriptScore transcript:student.getTranscriptList()){
                    if (transcript.getSubjectsId().equals(3L)){
                        transcript.setScore(scoreTemp);
                        transcript.setScoreType(scoreTypeTemp);
                    }
                }
            }
        }*/

        //七年级秋季班录入的成绩为市模拟成绩
        Integer examNum = 3;//期末
        String examName = "期末";
        if (yearClassId.equals(7) && quarter.getNum().equals(4)) {
            if (examNum.equals(3)) {
                examNum = 5;
                examName = "市模拟";
            }
        }


        //上个学期期末
        for (ResTranscriptScore transcript : student.getTranscriptList()) {

            transcript.setExamNum(examNum);
            QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();
            transcriptQueryWrapper.eq("year_part", lastQuarter.getYearPart());
            transcriptQueryWrapper.eq("quarter_num", lastQuarter.getNum());
            transcriptQueryWrapper.eq("subjects_id", transcript.getSubjectsId());
            transcriptQueryWrapper.eq("student_id", student.getId());
            transcriptQueryWrapper.eq("exam_num", examNum);
            transcriptQueryWrapper.last("limit 1");
            Transcript transcriptOne = transcriptMapper.selectOne(transcriptQueryWrapper);
            if (transcriptOne == null) {
                transcriptOne = new Transcript();
            } else {
                if (transcriptOne.getScoreStart() != null && transcriptOne.getScoreStart().compareTo(BigDecimal.ZERO) > 0) {
                    //已经填写过分数的，直接跳过
                    //continue;
                }
            }
            transcriptOne.setSchoolId(student.getSchoolId());
            Organization organization = iOrganizationService.getById(transcriptOne.getSchoolId());
            if (organization != null) {
                transcriptOne.setSchoolName(organization.getName());
            }
            transcriptOne.setReadingSchoolId(student.getReadingSchoolId());
            transcriptOne.setStudentId(student.getId());
            transcriptOne.setStudentName(student.getName());
            transcriptOne.setSubjectsId(transcript.getSubjectsId());
            transcriptOne.setYearPart(lastQuarter.getYearPart());
            transcriptOne.setQuarterNum(lastQuarter.getNum());
            transcriptOne.setExamNum(examNum);
            transcriptOne.setExamName(examName);
            transcriptOne.setYearClassId(transcriptYearClassId);
            transcriptOne.setScore(transcript.getScore());
            if (transcriptYearClassId != null) {
                String yearClassName = CommonUtil.getYearClassNameForId(transcriptYearClassId);
                transcriptOne.setYearClassName(yearClassName);
            }
            if (student.getSubjectsId() != null) {
                String subjectsName = CommonUtil.getSubjectsNameForId(Integer.parseInt(student.getSubjectsId().toString()));
                transcriptOne.setSubjectsName(subjectsName);
            }
            String quarterName = CommonUtil.getQuarterNameForId(lastQuarter.getNum());
            transcriptOne.setQuarterName(quarterName);

            Integer gradeSection = CommonUtil.getGradeSectionForYearClassId(transcriptYearClassId);
            if (StringUtils.isEmpty(transcript.getScore())) {
                continue;
            } else {
                if (transcript.getScore() != null && transcript.getScore().equals("0")) {
                    continue;
                }
                if (validateNumber(transcript.getScore())) {
                    BigDecimal score = new BigDecimal(transcript.getScore());
                    transcriptOne.setScoreStart(score);
                    transcriptOne.setScoreEnd(score);
                    transcriptOne.setScoreType(1);//分数类型：1为数字，2为字母
                } else {
                    StudentAtSchool studentAtSchool = iStudentAtSchoolService.getById(student.getReadingSchoolId());
                    QueryWrapper<StudentAtSchoolScoreDivide> studentAtSchoolScoreDivideQueryWrapper = new QueryWrapper<>();
                    studentAtSchoolScoreDivideQueryWrapper.eq("student_at_school_id", readingSchoolId);
                    studentAtSchoolScoreDivideQueryWrapper.eq("grade_section", gradeSection);
                    studentAtSchoolScoreDivideQueryWrapper.eq("score", transcript.getScore().trim());
                    studentAtSchoolScoreDivideQueryWrapper.last("limit 1");
                    StudentAtSchoolScoreDivide studentAtSchoolScoreDivide = iStudentAtSchoolScoreDivideService.getOne(studentAtSchoolScoreDivideQueryWrapper);
                    transcriptOne.setScoreStart(studentAtSchoolScoreDivide.getScoreStart());
                    transcriptOne.setScoreEnd(studentAtSchoolScoreDivide.getScoreEnd());
                    transcriptOne.setScoreType(2);//分数类型：1为数字，2为字母
                }
                transcriptOne.setIsStatistics(1);
            }
            //秋季初二班物理不添加
           /* if (quarter.equals(4) && yearClassId.equals(11) && transcript.getSubjectsId().equals(3L)) {
            } else {
            }*/
            this.saveOrUpdate(transcriptOne);

            //分层
            ChaStudyPlan chaStudyPlan = new ChaStudyPlan();
            chaStudyPlan.setYearPart(lastQuarter.getYearPart());
            chaStudyPlan.setQuarterNum(lastQuarter.getNum());
            chaStudyPlan.setStudentId(student.getId());
            chaStudyPlan.setSubjectsId(transcript.getSubjectsId());
            iMqPushService.chaStudyPlanAccuracy(chaStudyPlan);

            boolean isAddStudentClassType = true;
            //学期班才有期中成绩
            if (quarter.getType().equals(1) && student.getMidtermTranscriptList() != null && student.getMidtermTranscriptList().size() > 0) {
                for (ResTranscriptScore midtermTranscript : student.getMidtermTranscriptList()) {
                    if (midtermTranscript.getSubjectsId().equals(transcript.getSubjectsId())) {
                        if (midtermTranscript.getScore() != null && !midtermTranscript.getScore().equals("0")) {
                            isAddStudentClassType = false;//存在填写期中成功
                        }
                    }
                }
            }
            if (isAddStudentClassType) {
                //分层调整，因此注释掉
                //this.addStudentClassType(student, quarter, transcript, yearClassId);
            }

        }

        //学期班才有期中成绩
        if (quarter.getType().equals(1) && student.getMidtermTranscriptList() != null) {
            //当前学期期中
            for (ResTranscriptScore transcript : student.getMidtermTranscriptList()) {
                examNum = 1;//期中
                transcript.setExamNum(examNum);
                QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();
                transcriptQueryWrapper.eq("year_part", quarter.getYearPart());
                transcriptQueryWrapper.eq("quarter_num", quarter.getNum());
                transcriptQueryWrapper.eq("subjects_id", transcript.getSubjectsId());
                transcriptQueryWrapper.eq("student_id", student.getId());
                transcriptQueryWrapper.eq("exam_num", examNum);//期中
                transcriptQueryWrapper.last("limit 1");
                Transcript transcriptOne = transcriptMapper.selectOne(transcriptQueryWrapper);
                if (transcriptOne == null) {
                    transcriptOne = new Transcript();
                } else {
                    if (transcriptOne.getScoreStart() != null && transcriptOne.getScoreStart().compareTo(BigDecimal.ZERO) > 0) {
                        //已经填写过分数的，直接跳过
                        //continue;
                    }
                }
                transcriptOne.setSchoolId(student.getSchoolId());
                Organization organization = iOrganizationService.getById(transcriptOne.getSchoolId());
                if (organization != null) {
                    transcriptOne.setSchoolName(organization.getName());
                }
                transcriptOne.setReadingSchoolId(student.getReadingSchoolId());
                transcriptOne.setStudentId(student.getId());
                transcriptOne.setStudentName(student.getName());
                transcriptOne.setSubjectsId(transcript.getSubjectsId());
                transcriptOne.setYearPart(quarter.getYearPart());
                transcriptOne.setQuarterNum(quarter.getNum());
                transcriptOne.setExamNum(examNum);
                transcriptOne.setExamName("期中");
                transcriptOne.setYearClassId(yearClassId);
                transcriptOne.setScore(transcript.getScore());
                if (yearClassId != null) {
                    String yearClassName = CommonUtil.getYearClassNameForId(yearClassId);
                    transcriptOne.setYearClassName(yearClassName);
                }
                if (student.getSubjectsId() != null) {
                    String subjectsName = CommonUtil.getSubjectsNameForId(Integer.parseInt(student.getSubjectsId().toString()));
                    transcriptOne.setSubjectsName(subjectsName);
                }
                String quarterName = CommonUtil.getQuarterNameForId(quarter.getNum());
                transcriptOne.setQuarterName(quarterName);

                Integer gradeSection = CommonUtil.getGradeSectionForYearClassId(yearClassId);
                if (StringUtils.isEmpty(transcript.getScore())) {
                    continue;
                } else {
                    if (transcript.getScore() != null && transcript.getScore().equals("0")) {
                        continue;
                    }
                    if (validateNumber(transcript.getScore())) {
                        BigDecimal score = new BigDecimal(transcript.getScore());
                        transcriptOne.setScoreStart(score);
                        transcriptOne.setScoreEnd(score);
                        transcriptOne.setScoreType(1);//分数类型：1为数字，2为字母
                    } else {
                        StudentAtSchool studentAtSchool = iStudentAtSchoolService.getById(student.getReadingSchoolId());
                        QueryWrapper<StudentAtSchoolScoreDivide> studentAtSchoolScoreDivideQueryWrapper = new QueryWrapper<>();
                        studentAtSchoolScoreDivideQueryWrapper.eq("student_at_school_id", readingSchoolId);
                        studentAtSchoolScoreDivideQueryWrapper.eq("grade_section", gradeSection);
                        studentAtSchoolScoreDivideQueryWrapper.eq("score", transcript.getScore().trim());
                        studentAtSchoolScoreDivideQueryWrapper.last("limit 1");
                        StudentAtSchoolScoreDivide studentAtSchoolScoreDivide = iStudentAtSchoolScoreDivideService.getOne(studentAtSchoolScoreDivideQueryWrapper);
                        transcriptOne.setScoreStart(studentAtSchoolScoreDivide.getScoreStart());
                        transcriptOne.setScoreEnd(studentAtSchoolScoreDivide.getScoreEnd());
                        transcriptOne.setScoreType(2);//分数类型：1为数字，2为字母
                    }
                    transcriptOne.setIsStatistics(1);
                }
                //秋季初二班物理不添加
               /* if (quarter.equals(4) && yearClassId.equals(11) && transcript.getSubjectsId().equals(3L)) {
                } else {
                }*/
                this.saveOrUpdate(transcriptOne);
                //分层
                ChaStudyPlan chaStudyPlan = new ChaStudyPlan();
                chaStudyPlan.setYearPart(quarter.getYearPart());
                chaStudyPlan.setQuarterNum(quarter.getNum());
                chaStudyPlan.setStudentId(student.getId());
                chaStudyPlan.setSubjectsId(transcript.getSubjectsId());
                iMqPushService.chaStudyPlanAccuracy(chaStudyPlan);

                //this.addStudentClassType(student, quarter, transcript, yearClassId);
            }
        }

        return true;
    }

    @Override
    public void addStudentClassType(Student student, Quarter quarter, ResTranscriptScore transcript, Integer yearClassId) {
        Map<String, String> map;
        Integer gradeSection = CommonUtil.getGradeSectionForYearClassId(yearClassId);
        if (transcript.getExamNum() != null && transcript.getExamNum().equals(1)) {
            //期中
            map = this.getClassTypeMidterm(quarter, transcript.getSubjectsId(), gradeSection, transcript.getScore(), transcript.getScoreType(), yearClassId);
        } else {
            map = this.getClassType(quarter, transcript.getSubjectsId(), gradeSection, transcript.getScore(), transcript.getScoreType(), yearClassId);
        }
        String classTypeId = map.get("classTypeId");
        String classTypeIdStr = map.get("classTypeIdStr");
        QueryWrapper<StudentClassType> studentClassTypeQueryWrapper = new QueryWrapper<>();
        studentClassTypeQueryWrapper.eq("year_part", quarter.getYearPart());//当前学期
        studentClassTypeQueryWrapper.eq("quarter_num", quarter.getNum());
        studentClassTypeQueryWrapper.eq("student_id", student.getId());
        studentClassTypeQueryWrapper.eq("subjects_id", transcript.getSubjectsId());
        studentClassTypeQueryWrapper.last("limit 1");
        StudentClassType studentClassType = iStudentClassTypeService.getOne(studentClassTypeQueryWrapper);
        if (studentClassType == null) {
            studentClassType = new StudentClassType();
            studentClassType.setYearPart(quarter.getYearPart());
            studentClassType.setQuarterNum(quarter.getNum());
            studentClassType.setStudentId(student.getId());
            studentClassType.setSubjectsId(transcript.getSubjectsId());
            studentClassType.setClassTypeIdStr(classTypeIdStr);
            studentClassType.setClassTypeId(Long.parseLong(classTypeId));
            studentClassType.setYearClassId(yearClassId);
        } else {
            studentClassType.setClassTypeIdStr(classTypeIdStr);
            studentClassType.setClassTypeId(Long.parseLong(classTypeId));
            studentClassType.setYearClassId(yearClassId);
        }
        iStudentClassTypeService.saveOrUpdate(studentClassType);
    }


    /**
     * 期末
     *
     * @param quarter
     * @param subjectsId
     * @param gradeSection
     * @param score
     * @param scoreType
     * @return
     */
    public Map<String, String> getClassType(Quarter quarter, Long subjectsId, Integer gradeSection, String score, Integer scoreType, Integer yearClassId) {
        Long classTypeId = 1575859909042028546L;//默认提高班
        String classTypeIdStr = "1575859909042028546";
        Long classTypeIdNow = classTypeId;

        BigDecimal diffScorse = null;
        String diffScorseStr = null;//分段对比值
        CourseType courseType = null;
        Integer toType = 1;//要获取的班型类型，由于现在没有预备班，所以都为1

        //英语
        if (subjectsId.equals(1L)) {
            if (gradeSection.equals(0)) {
            } else if (gradeSection.equals(1)) {
                //分数类型：1分数，2分段
                if (scoreType.equals(1)) {
                    diffScorse = new BigDecimal(90);
                } else {
                    diffScorseStr = "A,A+";
                }
            } else if (gradeSection.equals(2)) {
                //分数类型：1分数，2分段
                if (scoreType.equals(1)) {
                    diffScorse = new BigDecimal(90);
                } else {
                    diffScorseStr = "A,A+";
                }
            } else if (gradeSection.equals(3)) {
                //分数类型：1分数，2分段
                //高一秋季班统一为提高班
                if (quarter.getNum().equals(4) && yearClassId.equals(10)) {

                } else {
                    if (scoreType.equals(1)) {
                        diffScorse = new BigDecimal(90);
                    } else {
                        diffScorseStr = "A,A+";
                    }
                }
            }
        } else if (subjectsId.equals(2L)) {
            //数学
            if (gradeSection.equals(0)) {

            } else if (gradeSection.equals(1)) {
                //分数类型：1分数，2分段
                if (scoreType.equals(1)) {
                    diffScorse = new BigDecimal(90);
                } else {
                    diffScorseStr = "A,A+";
                }
            } else if (gradeSection.equals(2)) {
                //分数类型：1分数，2分段
                if (scoreType.equals(1)) {
                    diffScorse = new BigDecimal(100);
                } else {
                    diffScorseStr = "A,A+";
                }
            } else if (gradeSection.equals(3)) {
                //高一
                if (quarter.getNum().equals(4) && yearClassId.equals(10)) {
                    if (scoreType.equals(1)) {
                        diffScorse = new BigDecimal(100);
                    } else {
                        diffScorseStr = "A,A+";
                    }
                } else {
                    if (scoreType.equals(1)) {
                        diffScorse = new BigDecimal(70);
                    } else {
                        diffScorseStr = "A,A+";
                    }
                }
            }
        } else if (subjectsId.equals(3L)) {
            if (gradeSection.equals(2)) {
                if (scoreType.equals(1)) {
                    //初二秋季班第一学期
                    if (quarter.getNum().equals(4) && yearClassId.equals(8)) {
                        diffScorse = new BigDecimal(100);
                    } else {
                        diffScorse = new BigDecimal(100);
                    }
                }
            } else if (gradeSection.equals(3)) {
                //分数类型：1分数，2分段
                //秋季班统一为提高班
                if (quarter.getNum().equals(4) && yearClassId.equals(10)) {
                    if (scoreType.equals(1)) {
                        diffScorse = new BigDecimal(90);
                    }
                } else {
                    if (scoreType.equals(1)) {
                        diffScorse = new BigDecimal(60);
                    }
                }
            }
        }
        if (scoreType.equals(1)) {
            if (diffScorse != null) {
                BigDecimal bScorese = new BigDecimal(score);
                if (bScorese.compareTo(diffScorse) >= 0) {
                    //学期
                    courseType = iCourseTypeService.getNextCourseType(classTypeId, toType);
                    if (courseType != null) {
                        classTypeIdStr += "," + courseType.getId();
                        classTypeIdNow = courseType.getId();
                    }
                }
            }
        } else {
            if (diffScorseStr != null) {
                List<String> diffScorseList = Arrays.asList(diffScorseStr.split(","));
                if (diffScorseList.contains(score)) {
                    //学期
                    courseType = iCourseTypeService.getNextCourseType(classTypeId, toType);
                    if (courseType != null) {
                        classTypeIdStr += "," + courseType.getId();
                        classTypeIdNow = courseType.getId();
                    }
                }
            }
        }
        //英语小学都是提高班
        if (subjectsId.equals(1L)) {
            /*if (gradeSection.equals(1)) {
                classTypeIdNow = classTypeId;
                classTypeIdStr = classTypeIdNow.toString();
            }*/
            classTypeIdNow = classTypeId;
            classTypeIdStr = classTypeIdNow.toString();
        }


        Map<String, String> map = new HashMap<>();
        map.put("classTypeIdStr", classTypeIdStr);
        map.put("classTypeId", classTypeIdNow.toString());
        return map;
    }


    /**
     * 期中
     *
     * @param quarter
     * @param subjectsId
     * @param gradeSection
     * @param score
     * @param scoreType
     * @param yearClassId
     * @return
     */
    public Map<String, String> getClassTypeMidterm(Quarter quarter, Long subjectsId, Integer gradeSection, String score, Integer scoreType, Integer yearClassId) {
        Long classTypeId = 1575859909042028546L;//默认提高班
        String classTypeIdStr = "1575859909042028546";
        Long classTypeIdNow = classTypeId;

        BigDecimal diffScorse = null;
        String diffScorseStr = null;//分段对比值
        CourseType courseType = null;
        Integer toType = 1;//要获取的班型类型，由于现在没有预备班，所以都为1

        //英语
        if (subjectsId.equals(1L)) {
            if (gradeSection.equals(0)) {
            } else if (gradeSection.equals(1)) {
                //分数类型：1分数，2分段
                if (scoreType.equals(1)) {
                    diffScorse = new BigDecimal(90);
                } else {
                    diffScorseStr = "A,A+";
                }
            } else if (gradeSection.equals(2)) {
                //分数类型：1分数，2分段
                if (scoreType.equals(1)) {
                    diffScorse = new BigDecimal(90);
                } else {
                    diffScorseStr = "A,A+";
                }
            } else if (gradeSection.equals(3)) {
                //分数类型：1分数，2分段
                //高一秋季班统一为提高班
                if (quarter.getNum().equals(4) && yearClassId.equals(10)) {

                } else {
                    if (scoreType.equals(1)) {
                        diffScorse = new BigDecimal(90);
                    } else {
                        diffScorseStr = "A,A+";
                    }
                }
            }
        } else if (subjectsId.equals(2L)) {
            //数学
            if (gradeSection.equals(0)) {

            } else if (gradeSection.equals(1)) {
                //分数类型：1分数，2分段
                if (scoreType.equals(1)) {
                    diffScorse = new BigDecimal(90);
                } else {
                    diffScorseStr = "A,A+";
                }
            } else if (gradeSection.equals(2)) {
                //分数类型：1分数，2分段
                if (scoreType.equals(1)) {
                    diffScorse = new BigDecimal(100);
                } else {
                    diffScorseStr = "A,A+";
                }
            } else if (gradeSection.equals(3)) {
                //分数类型：1分数，2分段
                //秋季班高一
                if (quarter.getNum().equals(4) && yearClassId.equals(10)) {
                    if (scoreType.equals(1)) {
                        diffScorse = new BigDecimal(70);
                    } else {
                        diffScorseStr = "A,A+";
                    }
                } else {
                    if (scoreType.equals(1)) {
                        diffScorse = new BigDecimal(70);
                    } else {
                        diffScorseStr = "A,A+";
                    }
                }
            }
        } else if (subjectsId.equals(3L)) {
            if (gradeSection.equals(2)) {
                if (scoreType.equals(1)) {
                    //初二秋季班第一学期
                    if (quarter.getNum().equals(4) && yearClassId.equals(8)) {
                        diffScorse = new BigDecimal(90);
                    } else {
                        diffScorse = new BigDecimal(90);
                    }
                }
            } else if (gradeSection.equals(3)) {
                //分数类型：1分数，2分段
                //秋季班统一为提高班
                if (quarter.getNum().equals(4) && yearClassId.equals(10)) {
                    if (scoreType.equals(1)) {
                        diffScorse = new BigDecimal(60);
                    }
                } else {
                    if (scoreType.equals(1)) {
                        diffScorse = new BigDecimal(60);
                    }
                }
            }
        }
        if (scoreType.equals(1)) {
            if (diffScorse != null) {
                BigDecimal bScorese = new BigDecimal(score);
                if (bScorese.compareTo(diffScorse) >= 0) {
                    //学期
                    courseType = iCourseTypeService.getNextCourseType(classTypeId, toType);
                    if (courseType != null) {
                        classTypeIdStr += "," + courseType.getId();
                        classTypeIdNow = courseType.getId();
                    }
                }
            }
        } else {
            if (diffScorseStr != null) {
                List<String> diffScorseList = Arrays.asList(diffScorseStr.split(","));
                if (diffScorseList.contains(score)) {
                    //学期
                    courseType = iCourseTypeService.getNextCourseType(classTypeId, toType);
                    if (courseType != null) {
                        classTypeIdStr += "," + courseType.getId();
                        classTypeIdNow = courseType.getId();
                    }
                }
            }
        }
        //英语小学都是提高班
        if (subjectsId.equals(1L)) {
            if (gradeSection.equals(1)) {
                classTypeIdNow = classTypeId;
                classTypeIdStr = classTypeIdNow.toString();
            }
        }


        Map<String, String> map = new HashMap<>();
        map.put("classTypeIdStr", classTypeIdStr);
        map.put("classTypeId", classTypeIdNow.toString());
        return map;
    }


    /**
     * 根据分数获取班型(2024秋季班调整前的接口)
     *
     * @param quarter
     * @param subjectsId
     * @param gradeSection
     * @param score
     * @param scoreType
     * @return
     */
    public Map<String, String> getClassType20244(Quarter quarter, Long subjectsId, Integer gradeSection, String score, Integer scoreType, Integer yearClassId) {
        Long classTypeId = 1575859909042028546L;//默认提高班
        String classTypeIdStr = "1575859909042028546";

        BigDecimal diffScorse = null;
        String diffScorseStr = null;//分段对比值
        CourseType courseType = null;

        //英语
        if (subjectsId.equals(1L)) {
            if (gradeSection.equals(0)) {
            } else if (gradeSection.equals(1)) {
                //分数类型：1分数，2分段
                if (scoreType.equals(1)) {
                    diffScorse = new BigDecimal(90);
                } else {
                    diffScorseStr = "A,A+";
                }
            } else if (gradeSection.equals(2)) {
                //分数类型：1分数，2分段
                if (scoreType.equals(1)) {
                    diffScorse = new BigDecimal(90);
                } else {
                    diffScorseStr = "A,A+";
                }
            } else if (gradeSection.equals(3)) {
                //分数类型：1分数，2分段
                //高一秋季班统一为提高班
                if (quarter.getNum().equals(4) && yearClassId.equals(10)) {

                } else {
                    if (scoreType.equals(1)) {
                        diffScorse = new BigDecimal(90);
                    } else {
                        diffScorseStr = "A,A+";
                    }
                }
            }
        } else if (subjectsId.equals(2L)) {
            //数学
            if (gradeSection.equals(0)) {

            } else if (gradeSection.equals(1)) {
                //分数类型：1分数，2分段
                if (scoreType.equals(1)) {
                    diffScorse = new BigDecimal(80);
                } else {
                    diffScorseStr = "A,A+";
                }
            } else if (gradeSection.equals(2)) {
                //分数类型：1分数，2分段
                if (scoreType.equals(1)) {
                    diffScorse = new BigDecimal(90);
                } else {
                    diffScorseStr = "A,A+";
                }
            } else if (gradeSection.equals(3)) {
                //分数类型：1分数，2分段
                //秋季班高一
                if (quarter.getNum().equals(4) && yearClassId.equals(10)) {
                } else {
                    if (scoreType.equals(1)) {
                        diffScorse = new BigDecimal(70);
                    } else {
                        diffScorseStr = "A,A+";
                    }
                }
            }
        } else if (subjectsId.equals(3L)) {
            if (gradeSection.equals(2)) {
                if (scoreType.equals(1)) {
                    //初二秋季班第一学期
                    if (quarter.getNum().equals(4) && yearClassId.equals(8)) {
                        diffScorse = new BigDecimal(84);
                    } else {
                        diffScorse = new BigDecimal(80);
                    }
                }
            } else if (gradeSection.equals(3)) {
                //分数类型：1分数，2分段
                //秋季班统一为提高班
                if (quarter.getNum().equals(4) && yearClassId.equals(10)) {

                } else {
                    if (scoreType.equals(1)) {
                        diffScorse = new BigDecimal(70);
                    }
                }
            }
        }
        if (scoreType.equals(1)) {
            if (diffScorse != null) {

                BigDecimal bScorese = new BigDecimal(score);
                if (bScorese.compareTo(diffScorse) >= 0) {
                    //学期
                    courseType = iCourseTypeService.getNextCourseType(classTypeId, quarter.getType());
                    if (courseType != null) {
                        classTypeIdStr += "," + classTypeId;
                    }
                }
            }
        } else {
            if (diffScorseStr != null) {
                List<String> diffScorseList = Arrays.asList(diffScorseStr.split(","));
                if (diffScorseList.contains(score)) {
                    //学期
                    courseType = iCourseTypeService.getNextCourseType(classTypeId, quarter.getType());
                    if (courseType != null) {
                        classTypeIdStr += "," + classTypeId;
                    }
                }
            }
        }

        Map<String, String> map = new HashMap<>();
        map.put("classTypeIdStr", classTypeIdStr);
        map.put("classTypeId", classTypeId.toString());
        return map;
    }


    public List<ResTranscriptScore> getStudentScore(Integer yearPart, Integer quarterNum, Long studentId, Integer examNum) {
        return transcriptMapper.getStudentScore(yearPart, quarterNum, studentId, examNum);
    }

    public List<TranscriptScoreExcel> getTranscriptList(Long schoolId, Integer yearPart, Integer quarterNum, Integer yearPartTranscript, Integer quarterNumTranscript, Integer examNum) {
        return transcriptMapper.getTranscriptStudentList(schoolId, yearPart, quarterNum, yearPartTranscript, quarterNumTranscript, examNum);
    }

    /**
     * 获取在读学员成绩信息
     *
     * @param yearPart        学员所在班级的年份
     * @param quarterNum      学员所在班级的学期
     * @param yearPartWrite   家长填写成绩的年份
     * @param quarterNumWrite 家长填写成功的学期
     * @param examNum         考试名称id
     * @param isStatistics    0为未填写，1为已填写
     * @return
     */
    @Override
    public List<ScoreData> getReadStudent(Integer yearPart, Integer quarterNum, Integer yearPartWrite, Integer quarterNumWrite, Integer examNum, String isStatistics, List<Integer> yearClassIdList) {
        QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(isStatistics), "is_statistics", isStatistics);
        if (yearClassIdList.size() > 0) {
            transcriptQueryWrapper.in("year_class_id", yearClassIdList);
        }

        return transcriptMapper.getReadStudent(yearPart, quarterNum, yearPartWrite, quarterNumWrite, examNum, transcriptQueryWrapper);
    }

    /**
     * 填写成绩推送
     *
     * @return
     */
    @Override
    @Async("onlineAsync")
    public void writeTranscriptPush(Integer yearPart, Integer quarterNum, Integer examNum, String isStatistics, Quarter quarter, List<Integer> yearClassIdList, Integer weicharPushCount, TranscriptSet transcriptSet) throws InterruptedException {

        if (transcriptSet.getPushType().equals(1)) {
            //获取当前
            //Quarter quarterNow = iQuarterService.getNowQuarter();
            List<ScoreData> scoreDataList = this.getReadStudent(yearPart, quarterNum, yearPart, quarterNum, examNum, isStatistics, yearClassIdList);
            Integer i = 1;
            for (ScoreData scoreData : scoreDataList) {
                if (scoreData.getIsStatistics().equals(0)) {
                    iWeiChartService.pushAction(scoreData.getStudentId(), quarter, weicharPushCount, transcriptSet);
                }
                //System.out.println("====i:" + i++);
            }
        } else {
            iWeiChartService.pushAction(0L, quarter, weicharPushCount, transcriptSet);
        }

    }

    /**
     * 人员分析安全值
     *
     * @param schoolId
     * @param subjectsId
     * @param yearPart
     * @param quarterNum
     * @return
     */
    @Override
    public List<HashMap<String, Object>> securityData(String schoolId, String subjectsId, String yearPart, String quarterNum, String teacherStr, String gradeSection) {
        Integer upYearPart = Integer.parseInt(yearPart);
        Integer upQuarterNum = 0;
        if (quarterNum.equals("2")) {
            upQuarterNum = 4;
            upYearPart = upYearPart - 1;
        } else if (quarterNum.equals("4")) {
            upQuarterNum = 2;
        } else if (quarterNum.equals("1")) {
            upQuarterNum = 4;
            upYearPart = upYearPart - 1;
        } else if (quarterNum.equals("3")) {
            upQuarterNum = 2;
        }

        String yearClassId = "";
        if (gradeSection.equals("1")) {
            yearClassId = "3,4,5,6";
        } else {
            yearClassId = "7,8,9";
        }

        QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();

        transcriptQueryWrapper.groupBy("teacher_id");
        transcriptQueryWrapper.groupBy("grade_section");
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        transcriptQueryWrapper.inSql("teacher_id", teacherStr);
        transcriptQueryWrapper.inSql(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        transcriptQueryWrapper.eq("grade_section", gradeSection);
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);

        Quarter quarter = iQuarterService.getQuarterForYearAndNum(Integer.parseInt(yearPart), Integer.parseInt(quarterNum));
        TranscriptSafetyScoreSet tsSet11 = new TranscriptSafetyScoreSet();//英语小学
        TranscriptSafetyScoreSet tsSet12 = new TranscriptSafetyScoreSet();//英语初中
        TranscriptSafetyScoreSet tsSet21 = new TranscriptSafetyScoreSet();//数学小学
        TranscriptSafetyScoreSet tsSet22 = new TranscriptSafetyScoreSet();//数学初中
        TranscriptSafetyScoreSet tsSet32 = new TranscriptSafetyScoreSet();//物理初中

        QueryWrapper<TranscriptSafetyScoreSet> transcriptSafetyScoreSetQueryWrapper = new QueryWrapper<>();
        transcriptSafetyScoreSetQueryWrapper.le("quarter_id_start", quarter.getId());
        transcriptSafetyScoreSetQueryWrapper.ge("quarter_id_end", quarter.getId());
        List<TranscriptSafetyScoreSet> transcriptSafetyScoreSetList = iTranscriptSafetyScoreSetService.list(transcriptSafetyScoreSetQueryWrapper);
        for (TranscriptSafetyScoreSet transcriptSafetyScoreSet : transcriptSafetyScoreSetList) {
            if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet11 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(1L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet12 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(1)) {
                tsSet21 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(2L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet22 = transcriptSafetyScoreSet;
            } else if (transcriptSafetyScoreSet.getSubjectsId().equals(3L) && transcriptSafetyScoreSet.getGradeSection().equals(2)) {
                tsSet32 = transcriptSafetyScoreSet;
            }
        }

        List<ScoreData> scoreDataList = transcriptMapper.securityData(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), 1, upYearPart, upQuarterNum, 3, transcriptQueryWrapper, tsSet11, tsSet12, tsSet21, tsSet22, tsSet32);

        List<HashMap<String, Object>> hashMapList = new ArrayList<>();

        for (ScoreData scoreData : scoreDataList) {
            HashMap hashMap = new HashMap<>();

            hashMap.put("teacherId", scoreData.getTeacherId());
            hashMap.put("scoreData", 0);
            if (scoreData.getStudentAllCount() > 0) {
                if (scoreData.getStuRealityCount() > 0) {
                    BigDecimal passRate = new BigDecimal(scoreData.getPassCount()).divide(new BigDecimal(scoreData.getStuRealityCount()), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
                    BigDecimal carryRate = new BigDecimal(scoreData.getCarryCount()).divide(new BigDecimal(scoreData.getStuRealityCount()), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
                    BigDecimal secureRate = carryRate.add(passRate);
                    hashMap.put("scoreData", secureRate);
                }
            }
            hashMapList.add(hashMap);
        }

        return hashMapList;
    }


    @Override
    public Integer updateIsEliminate(String isEliminate, String studentId, String quarterNum, String yearPart, String schoolId) {
        return transcriptMapper.updateIsEliminate(isEliminate, studentId, quarterNum, yearPart, schoolId);
    }

    @Override
    public Integer updateIsEliminateByYearClassId(String isEliminate, String yearClassId, String quarterNum, String yearPart) {
        return transcriptMapper.updateIsEliminateByYearClassId(isEliminate, yearClassId, quarterNum, yearPart);
    }

    @Override
    public IPage<ResNewStuScore> newStudentScore(IPage<?> page, String schoolId, String subjectsId, String yearClassId, String startDate, String endDate, String scoreYearPart, String scoreQuarterNum, String studentName) {

        IPage<ResNewStuScore> resNewStuScoreIPage = transcriptMapper.newStudentScore(page, schoolId, subjectsId, yearClassId, startDate, endDate, scoreYearPart, scoreQuarterNum, studentName);
        for (ResNewStuScore resNewStuScore : resNewStuScoreIPage.getRecords()) {
            resNewStuScore.setYearPart(Integer.parseInt(scoreYearPart));
            resNewStuScore.setQuarterNum(Integer.parseInt(scoreQuarterNum));
            resNewStuScore.setQuarterName(CommonUtil.getQuarterNameForId(Integer.parseInt(scoreQuarterNum)));
            resNewStuScore.setExamNum(3);
            resNewStuScore.setYearClassName(CommonUtil.getYearClassNameForId(resNewStuScore.getYearClassId()));
            resNewStuScore.setExamName(CommonUtil.getExamNameForId(resNewStuScore.getExamNum()));
        }
        return resNewStuScoreIPage;
    }


    @Override
    public Transcript getDataTranscript(Integer yearPart, Integer quarterNum, Long studentId, Long subjectsId, Integer examNum) {
        QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();
        transcriptQueryWrapper.eq("year_part", yearPart);
        transcriptQueryWrapper.eq("quarter_num", quarterNum);
        transcriptQueryWrapper.eq("exam_num", examNum);
        transcriptQueryWrapper.eq("student_id", studentId);
        transcriptQueryWrapper.eq("subjects_id", subjectsId);
        transcriptQueryWrapper.eq("is_statistics", 1);
        transcriptQueryWrapper.last("limit 1");
        Transcript transcriptOne = this.getOne(transcriptQueryWrapper);
        return transcriptOne;
    }

    /**
     * 判断学生成绩是否填写
     *
     * @param yearPart
     * @param quarterNum
     * @param subjectsId
     * @param studentId
     * @param yearClassId
     * @param examNum     1期中，3期末
     * @return
     */
    @Override
    public Long getApplyTranscriptIsIn(Integer yearPart, Integer quarterNum, Long subjectsId, Long studentId, Integer yearClassId, Integer examNum) {
        Long subjectsIdTranscript = subjectsId;//要查的科目分数是否录入，由于物理初二用的是初一数学
        Quarter lastQuarter = iQuarterService.getLastForTranscription(yearPart, quarterNum);
        if (subjectsId.equals(2L) && (yearClassId < 1 || (yearClassId == 1 && (quarterNum.equals(3) || quarterNum.equals(4))))) {
            return 1L;
        } else if (subjectsId.equals(1L) && (yearClassId < 3 || (yearClassId == 3 && (quarterNum.equals(3) || quarterNum.equals(4))))) {
            return 1L;
        } else if (subjectsId.equals(3L) && (yearClassId < 8 || (yearClassId == 8 && (quarterNum.equals(3) || quarterNum.equals(4))))) {
            subjectsIdTranscript = 2L;//物理查的是数据的有没有填写
        }

        QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();
        transcriptQueryWrapper.eq("year_part", lastQuarter.getYearPart());
        transcriptQueryWrapper.eq("quarter_num", lastQuarter.getNum());
        transcriptQueryWrapper.eq("subjects_id", subjectsIdTranscript);
        transcriptQueryWrapper.eq("exam_num", examNum);//3期末
        transcriptQueryWrapper.eq("student_id", studentId);
        transcriptQueryWrapper.eq("is_statistics", 1);//已填写
        Long transCount = this.count(transcriptQueryWrapper);
        return transCount;
    }


}
